 // pages/classify/classify.js
import {request} from "../../request/request.js";
// async
let regeneratorRuntim = require("../../utils/regenerator-runtime/runtime");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    leftMentList:[],
    rightconentList:[],
    currentIndex:0,
    scrollTop:0
  },
  CateData:[],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let CateData = wx.getStorageSync('cateData');
    if(!CateData){
      // 缓存中无数据，请求数据
      that.classifyList();
    }else{
      // 判断缓存数据有无过期
       if(Date.now()-CateData.time>10000*30){
             that.classifyList();
       }else{
        //  没过期不用发起请求,使用旧数据
         that.CateData = CateData.data;
         let leftMentList = that.CateData.map(v => v.cat_name);
         let rightconentList = that.CateData[0].children;
         that.setData({
           leftMentList,
           rightconentList
         })
       };

    }
    
  },
 async classifyList(){
    let that = this;
    let res = await request({url:'/categories'})
      that.CateData = res.data.message;
      wx.setStorageSync('cateData',{time:Date.now(),data:that.CateData});
      let leftMentList = that.CateData.map(v => v.cat_name);
      let rightconentList = that.CateData[0].children;
      that.setData({
        leftMentList,
        rightconentList,
      })
      console.log(res)
  },
  // 左侧菜单切换
  menuCut(e){
    let that = this;
       console.log(e)
       const {index} = e.currentTarget.dataset;
       let rightconentList = that.CateData[index].children;
       that.setData({
         currentIndex:index,
         rightconentList,
         scrollTop:0
       })

  },
  classifyDetails(e){
    console.log(e)
    wx.navigateTo({
      url: '../classifyList/classifyList',
    })
  }
})